package org.nobody.mallb2c.mallproduct.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import org.nobody.common.valid.AddGroup;
import org.nobody.common.valid.UpdateGroup;
import org.nobody.common.valid.UpdateStatusGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import org.nobody.mallb2c.mallproduct.entity.BrandEntity;
import org.nobody.mallb2c.mallproduct.service.BrandService;
import org.nobody.common.utils.PageUtils;
import org.nobody.common.utils.R;


/**
 * 品牌
 *
 * @author fancier
 * @email fancier2093@gmail.com
 * @date 2020-07-30 16:25:27
 */
@RestController
@RequestMapping("mallproduct/brand")
public class BrandController {
  @Autowired
  private BrandService brandService;

  /**
   * 列表
   */
  @RequestMapping("/list")
  //@RequiresPermissions("mallproduct:brand:list")
  public R list(@RequestParam Map<String, Object> params) {
    PageUtils page = brandService.queryPage(params);

    return R.ok().put("page", page);
  }


  /**
   * 信息
   */
  @RequestMapping("/info/{brandId}")
  //@RequiresPermissions("mallproduct:brand:info")
  public R info(@PathVariable("brandId") Long brandId) {
    BrandEntity brand = brandService.getById(brandId);

    return R.ok().put("brand", brand);
  }

  @GetMapping("/infos")
  public R info(@RequestParam("brandIds") List<Long> brandIds) {
    List<BrandEntity> brand = brandService.getBrandsByIds(brandIds);

    return R.ok().put("brand", brand);
  }

  /**
   * 保存
   */
  @RequestMapping("/save")
  //@RequiresPermissions("mallproduct:brand:save")
  public R save(@Validated({AddGroup.class}) @RequestBody BrandEntity brand/*, BindingResult result*//*←没有的话 出错则会自动向外抛错误*/) {

//      if(result.hasErrors()) {
//        Map<String, String> map = new HashMap<>();
//
//        result.getFieldErrors().forEach(item -> {
//          //FieldError获取到错误提示
//          String message = item.getDefaultMessage();
//          //获取错误的属性的名字
//          String field = item.getField();
//          map.put(field, message);
//        });
//
//        return R.error(400, "提交的数据不合法").put("data", map);
//      }

    brandService.save(brand);

    return R.ok();
  }

  /**
   * 修改
   */
  @RequestMapping("/update")
  //@RequiresPermissions("mallproduct:brand:update")
  public R update(@Validated({UpdateGroup.class}) @RequestBody BrandEntity brand) {
    brandService.updateDetail(brand);

    return R.ok();
  }

  /**
   * 专门用来修改状态的接口
   * @param brand
   * @return
   */
  @RequestMapping("/update/status")
  //@RequiresPermissions("mallproduct:brand:update")
  public R updateStatus(@Validated({UpdateStatusGroup.class}) @RequestBody BrandEntity brand) {
    brandService.updateById(brand);

    return R.ok();
  }

  /**
   * 删除
   */
  @RequestMapping("/delete")
  //@RequiresPermissions("mallproduct:brand:delete")
  public R delete(@RequestBody Long[] brandIds) {
    brandService.removeByIds(Arrays.asList(brandIds));

    return R.ok();
  }

}
